import * as asyncWx from "../../utils/asyncWx.js";
Page({
    data:{
        address:{},
        carts:[],
        allChecked:false,
        totalPrice:0,
        totalNum:0
    },
    //页面显示隐藏调用的回调函数
    onShow(){
        let address=wx.getStorageSync("address");
        let carts=wx.getStorageSync("carts") || [];
        this.setData({
            address
        });
        this.setCart(carts);
    },
    //点击收货按钮事件监听
    async getReceiveAddress(){
        // wx.getSetting({
        //     success(result){
        //         if(result.authSetting["scope.address"]===true || result.authSetting["scope.address"]===undefined){
        //             wx.chooseAddress({
        //                 success(res1){
        //                     console.log(res1);
        //                 }
        //             });
        //         }else{
        //             wx.openSetting({
        //                 success(res2){
        //                     wx.chooseAddress({
        //                         success(res3){
        //                             console.log(res3);
        //                         }
        //                     });
        //                 }
        //             });
        //         };
        //     }
        // });
        try{
            const res1=await asyncWx.getSetting();
            const scopeAddress=res1.authSetting["scope.address"];
            if(scopeAddress===false){
                await asyncWx.openSetting();
            };
            const res2=await asyncWx.chooseAddress();
            wx.setStorageSync("address",res2);//存入缓存
        }catch(error){
            console.log(error);
        };
    },
    //点击购物车复选框选中与否事件监听 
    changeCartGoods(e){
        let goods_id=e.currentTarget.dataset.id;
        this.data.carts.forEach((item)=>{
            if(item.goods_id===goods_id){
                item.checked=!item.checked;
            };
        });
        this.setCart(this.data.carts);
    },
    //点击全选按钮事件监听
    allSelectCartGoods(){
        this.data.allChecked=!this.data.allChecked;
        this.data.carts.forEach((item)=>{
            item.checked=this.data.allChecked;
        });
        this.setCart(this.data.carts);
    },
    //点击加减按钮事件监听
    async handleItemNumEdit(e){
        let {id,operate}=e.currentTarget.dataset;
        let index=this.data.carts.findIndex((item)=>item.goods_id===id);
        if(this.data.carts[index].num===1 && operate===-1){
            let res=await asyncWx.showModal({content:"确定将此商品从购物车移除吗？"});
            if(res.confirm){
                this.data.carts.splice(index,1);
            };
        }else{
            this.data.carts[index].num+=operate;
        };
        this.setCart(this.data.carts);
    },
    //点击结算按钮事件监听
    async handlePay(){
        const {address,totalNum}=this.data;
        if(!address.userName){
            await asyncWx.showToast({title:"您还没有添加收货地址",icon:"none"});
            return;
        }else if(totalNum==0){
            await asyncWx.showToast({title:"您还没有选择商品",icon:"none"});
            return;
        }else{
            wx.navigateTo({
                url: "/pages/pay/index",
                events:{
                    otherEvent:function(data){
                        console.log(data);
                    },
                    someEvent:function(data){
                        console.log(data);
                    }
                },
                success:function(res){
                    res.eventChannel.emit("acceptDataFromOpenedPage",{data:"qilin"});
                }
            });
        };
    },
    //设置复选框选中与否时相应data以及缓存中的数据变化
    setCart(carts){
        let allChecked=true;
        let totalPrice=0;
        let totalNum=0;
        carts.forEach((item)=>{
            if(item.checked){
                totalPrice+=item.num*item.goods_price;
                totalNum+=item.num;
            }else{
                allChecked=false;
            };
        });
        allChecked=carts.length?allChecked:false;//若carts为空数组时
        this.setData({
            carts,
            allChecked,
            totalPrice,
            totalNum
        });
        wx.setStorageSync("carts",carts);
    }
})